/**
* @Title: institution_detail_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-chenyonghong001
* @date 2014年10月17日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('doctorInfoQueryCtrl', ['$scope','CommunityService','$http','$modal','$state','$stateParams','doctorInfoService','BaseURL','Modal',
	                                function($scope,CommunityService,$http,$modal,$state,$stateParams,doctorInfoService,baseUrl,Modal){
     $scope.UrlParams = $stateParams;
     $scope.communityService = CommunityService;
     CommunityService.getBaseCode('{codeType:\'provincialDep\'}');
     CommunityService.getBaseCode('{codeType:\'sex\'}');
     CommunityService.getBaseCode('{codeType:\'cityDep\'}');
	 CommunityService.getBaseCode('{codeType:\'personTypeHos\'}');
	 CommunityService.getBaseCode('{codeType:\'personTypeShop\'}');
	 CommunityService.getBaseCode('{codeType:\'jobType\'}');
	 CommunityService.getBaseCode('{codeType:\'id_type\'}');
	 CommunityService.getBaseCode('{codeType:\'qualifiedLev\'}');
     $scope.authority={
     		add:1,
     		update:1,
     		deleted:1
     };
    
		// 表头以及json对应的属性名称
 	var cellTemplate = 
		'<td>'+
			'<a ng-bind="data.name" ng-click="queryDoctor(data.id)"></a>'+
		'</td>';
     
     $scope.columns = [  
			{
			label : '姓名',
			map : 'name',
			width : '20%',
			cellTemplate:cellTemplate
		}, {
			label : '人员分类',
			map : 'personType',
			width : '20%'
		}, {
			label : '所属科室',
			map : 'departName',
			width : '20%'
		}, {
			label : '手机号码',
			map : 'mobilePhone',
			width : '20%'
		}, {
			label : '电话号码',
			map : 'contactPhone',
			width : '20%'
		}];
     
		var start = 0,
		limit = 20;
		$scope.currentTab = null;
		$scope.tabs = [  {name:'基本信息',type:'institutionInfo.institutionDetail'},
			                {name:'医保服务人员',type:'institutionInfo.doctorInfoDetail'},
				               {name:'联系信息',type:'institutionInfo.relation'}
		            ];
		
		$scope.currentTab =$scope.tabs[1];
		//切换tab页
		
		$scope.tabClick = function(tab) {
			 $(window).trigger('resize');
			$state.go(tab.type,{
				providerid:$scope.UrlParams.providerid,
				type:$scope.UrlParams.type
    		});
		}
        $scope.queryDoctorInfoListParams = {
                page:"0",
                pageSize:"20"
            };
        
	$scope.$on('pageTool.skipPage',function(e,v){
			
			start = limit*(v-1);
			$scope.queryDoctorInfoListParams.page = start;
			$scope.onSubmit();
		});
        $scope.onSubmit = function(){
        	
          //  var pageInfo = $scope.pageInfo;
            $scope.queryDoctorInfoListParams.providerid = $scope.UrlParams.providerid;
                 var value=$scope.UrlParams.type;
                if(value=='0')
                {
                    $scope.queryDoctorInfoListParams.type='personTypeHos';
                }else
                {
                    $scope.queryDoctorInfoListParams.type='personTypeShop';
                }
            doctorInfoService.query($scope.queryDoctorInfoListParams).success(function(res){
                $scope.tableData = res.root;
                $scope.institutionDetail=res.provider;
                $scope.authority=res.authority;
                $scope.totalCount = res.total;
            });
        };

        //重置按钮事件
        $scope.onReset=function(){
            $scope.queryDoctorInfoListParams.name='';
            $scope.queryDoctorInfoListParams.personType='';
            $scope.queryDoctorInfoListParams.departNo='';
        };
       var	totalChecks = [];
       $scope.checkRows = [];

        function findCheck(d) {
            for(var i=0;i< $scope.checkRows .length;i++) {
                var checkedData =  $scope.checkRows [i];
                if(checkedData.itemId == d.itemId) {
                    return i;
                }
            }
            return -1;
        }

        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                $scope.checkRows .push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                $scope.checkRows.splice(index,1);
            }
        });
     $scope.queryDoctor = function (doctorId){
    		$state.go('institutionInfo.addDoctorInfo',{
    			providerid:$scope.UrlParams.providerid,
    			doctorId:doctorId,
    			type:$scope.UrlParams.type
    		});
     }
        
        /**
         * 机构详细信息--新增
         * @param params
         */
		//打开日期Model
		$scope.addDoctor = function () {
			$state.go('institutionInfo.addDoctorInfo',{
				providerid:$scope.UrlParams.providerid,
				type:$scope.UrlParams.type
    		});
		};
        
        /**
         *机构列表--删除
         * @param params
         */
        $scope.deleteDoctor = function(){
            var	checkRows =  $scope.checkRows;
            if (checkRows.length == 0) {
                Modal.alert("请勾选删除项！");
                return;
            }else
            {
                var ids = '';
                angular.forEach(checkRows,function(totalCheck,i){
                    ids+="'"+totalCheck.id+"',";
                });
                var  params= {
                ids : ids
                }
                Modal.confirm('确认删除吗？',function() {
                    $http.post(baseUrl + '/hcp/hcp/insurance/delete.do', params).success(function (data) {
                        if (data.state == '0') {
                            Modal.alert(data.msg);
                        } else {
                            Modal.alert("删除失败！");
                        }
                        $scope.onSubmit(2);
                    })
                })
            }
        };
        //导出csv文件
        $scope.exportDoctor=function(){
        var params = $scope.queryDoctorInfoListParams;
        var exportTotalCount = 0;
        $http({
            method:'post',
            cache: false,
            url:  baseUrl+'/hcp/hcp/insurance/exportInsuranceCount.do',
            params:params
        }).success(function(response){
            exportTotalCount = response.total;
            if(exportTotalCount && exportTotalCount > 30000){
                Modal.alert("数据量超过3万条不能导出，请重新筛选！");
                return;
            }else if(exportTotalCount==0)
            {
                Modal.alert("数据量为0条不能导出，请重新筛选！");
                return;
            }else
            {
                var url = baseUrl+"/hcp/hcp/insurance/exportInsurance.do?"+jQuery.param($scope.queryDoctorInfoListParams);
                window.location.href = url;
            }

        });

    }
        $scope.onSubmit();
    }
	]);
})();